package com.dqhl.community.util;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.CountDownTimer;
import android.widget.Button;


/**
 * Created by cheng on 2016/8/30.
 * 倒计时工具类
 */
public class TimeCountUtil extends CountDownTimer {

    private Activity mActivity;
    private Button btn;

    /**
     * 构造函数
     *
     * @param mActivity         activity
     * @param millisInFuture    总时间
     * @param countDownInterval 倒计时间隔
     * @param btn               button控件
     */
    public TimeCountUtil(Activity mActivity, long millisInFuture, long countDownInterval, Button btn) {
        super(millisInFuture, countDownInterval);
        this.mActivity = mActivity;
        this.btn = btn;
    }

    @SuppressLint("NewApi")
    @Override
    public void onTick(long millisUntilFinished) {
        String strSeconds = millisUntilFinished / 1000 + "s后重新获取";
        btn.setClickable(false); //设置不能点击
        //设置倒计时时间
        btn.setText(strSeconds);
        //设置按钮为灰色，这时是不能点击的
//        btn.setBackground(mActivity.getResources().getDrawable(R.drawable.bg_fill_gray));
        //获取按钮的文字
//        Spannable span = new SpannableString(btn.getText().toString());
        //将倒计时时间显示为红色
//        span.setSpan(new ForegroundColorSpan(Color.RED), 0, 2, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
//        btn.setText(span);
//        btn.setTextColor(mActivity.getResources().getColor(R.color.white));

    }

    @SuppressLint("NewApi")
    @Override
    public void onFinish() {
        btn.setText("获取验证码");
        btn.setClickable(true);
//        btn.setBackground(mActivity.getResources().getDrawable(R.drawable.btn_selector));
//        btn.setTextColor(mActivity.getResources().getColor(R.color.white));

    }
}
